package Euler22;

import java.io.*;
import java.util.*;

public class NameScores {
	public static void main(String[] args) {
		List<String> names = new ArrayList<String>();
		FileInputStream fis;
		InputStreamReader in;
		
		try {
			fis = new FileInputStream("names.txt");
			in = new InputStreamReader(fis);
			
			int next = 0;
			String name = "";
			
			while((next = in.read())!= -1)
			{
				char ch = (char) next;
				
				if(ch == ',')
				{
					names.add(name);
					name = "";
				}
				else if(ch == '\"')
				{
					continue;
				}
				else
				{
					name += ch;
				}
			}
			
			if(name.length()>0)
			{
				names.add(name);
				name = "";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Collections.sort(names);
		
		long score = 0;
		for(int i = 0; i < names.size(); i++)
		{
			score += getNameScore(names.get(i),i+1);
		}
		
		System.out.println(score);
	}
	
	static long getNameScore(String name, int position)
	{
		long score = 0;
		String temp = "";
		for(int i = 0; i < name.length(); i++)
		{
			int num = (name.charAt(i))-64;
			temp += num + " ";
			score += num;
		}
		//System.out.println(name + ": " + temp + "= " + score);
		
		return (score*position);
	}
}
